package thread.flag;

public class SuspendResume {
    public static void main(String[] args){
        NewThread ob1=new NewThread("One");
        NewThread ob2=new NewThread("Two");

        try {
            Thread.sleep(1000);
            ob1.mysuspend();
            System.out.println("Suspending thread One");
            Thread.sleep(1000);
            ob1.myresume();
            System.out.println("Resuming thread One");
            ob2.mysuspend();
            System.out.println("Suspending thread Two");
            Thread.sleep(1000);
            ob2.myresume();
            System.out.println("Resuming thread Two");
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        try {
            System.out.println("Waiting for threads to finish.");
            ob1.t.join();
            ob2.t.join();
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        System.out.println("Main thread exiting.");
    }


}


class NewThread implements Runnable{
    String name; //name of thread
    Thread t;
    boolean suspendFlag;

    NewThread(String threadname){
        this.name=threadname;
        t=new Thread(this,name);
        System.out.println("New thread: "+t);
        suspendFlag=false;
        t.start();//Start the thread
    }

    //This is the entry point for thread.

    @Override
    public void run() {
        try {
            for(int i=15;i>0;i--){
                System.out.println(name+": "+i);
                Thread.sleep(200);
                synchronized (this){
                    while (suspendFlag){
                        wait();
                    }
                }
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        System.out.println(name+" exiting.");
    }

    synchronized void mysuspend(){
        this.suspendFlag=true;
    }

    synchronized void myresume(){
        suspendFlag=false;
        notify();
    }

}